﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

namespace XFABManager
{

    /// <summary>
    /// 协程启动类(适用于非继承自MonoBehaviour的脚本启动协程)
    /// </summary>
    public class CoroutineStarter : MonoBehaviour
    {

        private static bool destroyed = false;

        private static CoroutineStarter _instance;

        private static CoroutineStarter Instance {
            get {

                #region UNITY_EDITOR
                if (!Application.isPlaying) return null;
                #endregion

                if (destroyed) return null;
                
                if ( _instance == null ) {
                    GameObject obj = new GameObject("CoroutineStarter");
                    obj.AddComponent<CoroutineStarter>();
                }
                return _instance;
            }
        }

        void Awake() {
            _instance = this;
            DontDestroyOnLoad(gameObject);
        }

        /// <summary>
        /// 启动协程（适用于在非继承自MonoBehaviour的脚本中启动协程）,且通过该方法启动的协程不会因为切换场景而停止
        /// </summary>
        /// <param name="enumerator"></param>
        public static Coroutine Start(IEnumerator enumerator) {
            if (Instance == null) {
                return null; 
            }
            return Instance.StartCoroutine(enumerator);
        }

        /// <summary>
        /// 停止由CoroutineStarter启动的协程 
        /// </summary>
        /// <param name="coroutine"></param>
        public static void Stop(Coroutine coroutine) {
            if (Instance == null) return;
            Instance.StopCoroutine(coroutine);
        }
         
        private void OnDestroy()
        {
            destroyed = true;
        }

    }

}

